<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

use Think\AjaxPage;

class PickController extends BaseController {

    /**
     * 自提点
     */
    public function index() {
        $proviceRs = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
        $this->assign('province', $proviceRs);
        $this->display();
    }

    /**
     * 获取自提数据
     */
    public function ajaxPickList() {
        $provinceId = I('post.province_id');
        $cityId = I('post.city_id');
        $districtId = I('post.district_id');
        $orderField = I('post.order_by_field', 'pick_id');
        $orderSort = I('post.order_by_mode', 'desc');
        $keyword = I('post.key_word');
        if (!empty($provinceId)) {
            $where['p.province_id'] = $provinceId;
        }
        if (!empty($cityId)) {
            $where['p.city_id'] = $cityId;
        }
        if (!empty($districtId)) {
            $where['p.district_id'] = $districtId;
        }
        if (!empty($keyword)) {
            $map['p.pick_name'] = array('like', $keyword);
        }

        $model = M('');
        $prefix = C('DB_PREFIX');
        $count = $model->table($prefix . 'pick p')->where($where)->count();
        $Page = new AjaxPage($count, 10);
        $show = $Page->show();

        $pickRs = $model->field('p.*,r1.name as province_name,r2.name as city_name,r3.name as district_name,s.supplier_name')
                ->table($prefix . 'pick p')
                ->join('LEFT JOIN ' . $prefix . 'region AS r1 ON r1.id = p.province_id')
                ->join('LEFT JOIN ' . $prefix . 'region AS r2 ON r2.id = p.city_id')
                ->join('LEFT JOIN ' . $prefix . 'region AS r3 ON r3.id = p.district_id')
                ->join('LEFT JOIN ' . $prefix . 'supplier AS s ON s.supplier_id = p.supplier_id')
                ->where($where)->order($orderField . ' ' . $orderSort)
                ->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('pickList', $pickRs);
        $this->assign('page', $show);
        $this->display();
    }

    public function add() {
        if (IS_POST) {
            $data = I('post.');
            $pickId = I('post.pick_id');
            $model = M('pick');
            if (empty($pickId)) {
                unset($pickId);
                $add_res = $model->add($data);
                if ($add_res === false) {
                    $this->error('添加失败', U('Admin/Pick/add'));
                } else {
                    $this->success('添加成功', U('Admin/Pick/index'));
                }
            } else {
                $updateRs = $model->where(array('pick_id' => $pickId))->save($data);
                if ($updateRs === false) {
                    $this->error('更新失败', U('Admin/Pick/editAddress', array('pick_id' => $pickId)));
                } else {
                    $this->success('更新成功', U('Admin/Pick/index'));
                }
            }
        }
        $supplierRs = M('supplier')->where(array('is_check' => 1))->select();
        $provinceRs = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
        $this->assign('province', $provinceRs);
        $this->assign('supplier', $supplierRs);
        $this->display();
    }

    /**
     * 地址编辑
     */
    public function editAddress() {
        $pickId = I('get.pick_id');
        $pick = M('pick')->where(array('pick_id' => $pickId))->find();
        $provinceRs = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
        $cityRs = M('region')->where(array('parent_id' => $pick['province_id'], 'level' => 2))->select();
        $distrctRs = M('region')->where(array('parent_id' => $pick['city_id'], 'level' => 3))->select();
        $supplierRs = M('supplier')->where(array('is_check' => 1))->select();

        $this->assign('province', $provinceRs);
        $this->assign('city', $cityRs);
        $this->assign('district', $distrctRs);
        $this->assign('supplier', $supplierRs);
        $this->assign('pick', $pick);
        $this->display('add');
    }

    /**
     * 删除自提点
     */
    public function del() {
        $pickupId = I('get.pickup_id');
        M('pick_up')->where(array('pickup_id' => $pickupId))->delete();
        $returnRs = array('status' => 1, 'msg' => '操作成功', 'data' => '',);
        $this->ajaxReturn(json_encode($returnRs));
    }

}
